package com.examples.ry.graphics;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class TransparentImages {
	public static final String resourceName = "images.gif";

	public static void main(String[] args) {
		final ImageData data = new ImageData(
				TransparentImages.class.getResourceAsStream(resourceName));
		final Display display = new Display();
		
		//Image 1		
		final Image img = new Image(display, data);
		//Image 2
		data.transparentPixel = -1;
		data.alpha = 150;
		final Image img2 = new Image(display, data);
		//Image 3
		data.alpha = -1;
		byte[] alphas = new byte[data.width];
		for (int i = 0; i < data.height; ++i) {
			Arrays.fill(alphas, (byte) (255.0 * i / data.height));
			data.setAlphas(data.x, data.y + i, data.width, alphas, 0);
		}
		final Image img3 = new Image(display, data);
		
		
		final Shell shell = new Shell(display, SWT.SHELL_TRIM);
		final Color gray = display.getSystemColor(SWT.COLOR_GRAY);
		shell.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				event.gc.setBackground(gray);
				event.gc.fillRectangle(shell.getClientArea());
				event.gc.drawImage(img, data.x, data.y, data.width,
						data.height, 10, 10, data.width, data.height);
				event.gc.drawImage(img2, data.x, data.y, data.width,
						data.height, 20 + data.width, 10, data.width,
						data.height);
				event.gc.drawImage(img3, data.x, data.y, data.width,
						data.height, 30 + (2 * data.width), 10, data.width,
						data.height);
			}
		});
		shell.setText("Load Transparent Images");
		shell.setSize(350, 180);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		img.dispose();
		img2.dispose();
		img3.dispose();
		display.dispose();
	}
}